import 'package:flutterapp/Base/InitConfig.dart';
import 'package:flutterapp/Base/methodChannel/MethodChannelManager.dart';
import 'package:flutterapp/Base/net/Api.dart';
import 'package:flutterapp/Base/net/HttpManager.dart';
import 'package:flutterapp/Base/router/RouterConfig.dart';
import 'package:flutterapp/Bean/RespLogin.dart';
import 'package:flutterapp/Pages/PageMain.dart';
import 'package:flutterapp/pages/PageLogin.dart';
import 'package:fluttertoast/fluttertoast.dart';

class LoginManager {
  static const TOKEN_KEY = "token";
  static const ACCOUNT_KEY = "account";
  static const PSW_KEY = "psw";

  static login(String userName, String psw) {
    if (userName == "" || userName == null || psw == "" || psw == null) {
      Fluttertoast.showToast(msg: "请正确填写账号或密码");
      return;
    }

    myHttp.post(Api.login, data: {"mobile": userName, "password": psw}).then(
        (value) async {
      RespLogin resp = RespLogin.fromJson(value);
      InitConfig.sPrefs.setString(TOKEN_KEY, resp.token).then((value) {
        print("setTokenSuccess-------" + getToken());
      });
      InitConfig.sPrefs.setString(ACCOUNT_KEY, userName);
      InitConfig.sPrefs.setString(PSW_KEY, psw);

      // await _getUserInfo();
      // JPushManager.loginPush("shop${Global.appState.shopInfo.shopId}");
      await MethodChannelManager.getInstance()
          .invoke(MethodTypes.SOCKET_CREATE, {
        "url": InitConfig.BASE_WS_URL,
        "token": LoginManager.getToken(),
        "user_id": resp.id,
        "user_name": resp.user_nickname,
        "user_mobile": resp.mobile,
        "agent_id": resp.role_id == 4 ? resp.recycler_agent_user_id : resp.agent_info.user_id,
        "wall_ids": resp.wall_ids,
        "role_id": resp.role_id
      });
      // todo 登录完成的操作
      Fluttertoast.showToast(msg: "登陆成功");
      if (resp.role_id == 4) {
        MethodChannelManager.getInstance().invoke(MethodTypes.START_LOCATING);
      }
      MyNavigatorObserver.getInstance()
          .navigator
          .pushNamedAndRemoveUntil(PageMain.ROUTER_NAME, (route) => false);
    });
  }

  // static silenceLogin() async {
  //   if (isLogin()) {
  //     await _getUserInfo();
  //     // JPushManager.loginPush("shop${Global.appState.shopInfo.shopId}");
  //     MethodChannelManager.getInstance().invoke(MethodTypes.SOCKET_CREATE,
  //         {"url": InitConfig.BASE_WS_URL, "token": LoginManager.getToken()});
  //     MethodChannelManager.getInstance().invoke(MethodTypes.START_LOCATING);
  //     MyNavigatorObserver.getInstance()
  //         .navigator
  //         .pushNamedAndRemoveUntil(PageMain.ROUTER_NAME, (route) => false);
  //   }
  // }

  static bool isLogin() {
    return getToken() != "";
  }

  static String getToken() {
    String token = InitConfig.sPrefs.getString(TOKEN_KEY) ?? "";
    print("token ===> $token");
    return token;
  }

  static logout() {
    InitConfig.sPrefs.setString(ACCOUNT_KEY, "");
    InitConfig.sPrefs.setString(PSW_KEY, "");
    // JPushManager.logoutPush();
    MethodChannelManager.getInstance().invoke(MethodTypes.STOP_LOCATING);
    MethodChannelManager.getInstance().invoke(MethodTypes.SOCKET_CLOSE);
    MyNavigatorObserver.getInstance()
        .navigator
        .pushNamedAndRemoveUntil(PageLogin.ROUTER_NAME, (route) => false);
  }

  static _getUserInfo() {
    // http.get(Api.getShopInfo).then((value) {
    //   print("DaoShopInfo ${DaoShopInfo.fromJson(value)}");
    //   Global.appStore
    //       .dispatch(PlayLoadShopInfo(DaoShopInfo.fromJson(value)));
    // });
  }
}
